package com.example.vaccine.controller;

import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.entity.VaccineFeedback;
import com.example.vaccine.entity.VaccineUsage;
import com.example.vaccine.service.VaccineFeedbackService;
import com.example.vaccine.service.VaccineUsageService;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doctor")
@Tag(name = "医生疫苗操作接口", description = "提供医生疫苗领取与反馈等功能")
public class DoctorVaccineController {

    @Autowired
    private VaccineUsageService usageService;

    @Autowired
    private VaccineFeedbackService feedbackService;

    @Operation(
            summary = "医生领取疫苗",
            description = "医生提交领取疫苗的信息",
            parameters = {
                    @Parameter(name = "doctorId", description = "医生ID", required = true, in = ParameterIn.HEADER)
            }
    )
    @PostMapping("/usage")
    public ResultVO receiveVaccine(
            @RequestBody VaccineUsage usage,
            @RequestHeader("doctorId") Integer doctorId) {
        usage.setDoctorId(doctorId);
        boolean result = usageService.receiveVaccine(usage);
        if (result) {
            return ResultVOUtil.success(null);
        } else {
            return ResultVOUtil.fail();
        }
    }


    @Operation(
            summary = "获取已领取疫苗列表",
            description = "根据医生ID获取该医生已领取的疫苗记录",
            parameters = {
                    @Parameter(name = "doctorId", description = "医生ID", required = true, in = ParameterIn.HEADER)
            }
    )
    @GetMapping("/usage/list")
    public ResultVO getUsageList(@RequestHeader("doctorId") Integer doctorId) {
        List<Map<String, Object>> list = usageService.getUsageRecords(doctorId);
        return ResultVOUtil.success(list);
    }

    @Operation(
            summary = "提交疫苗反馈",
            description = "医生提交使用疫苗后的反馈信息"
    )
    @PostMapping("/feedback")
    public ResultVO feedback(@RequestBody VaccineFeedback feedback) {
        boolean exists = feedbackService.existsByDoctorIdAndVaccineNumbering(
                feedback.getDoctorId(), feedback.getVaccineNumbering());
        if (exists) {
            return ResultVOUtil.fail(); // 可自定义错误码
        }
        boolean saved = feedbackService.save(feedback);
        return saved ? ResultVOUtil.success(null) : ResultVOUtil.fail();
    }
}
